* Author: Anina Schwarzenbach
* generated October 2014
* last review August 2020

clear
use "../../STATAfiles/FR_POLIS_31juillet14_FINAL&IRIS.dta"

*load ADOS
sysdir set PERSONAL "../../../ados"

set more off

*******************************************************************************************
*cleaning of last contact - FRANCE
******************************************************************************************
* variables
/*
tab Q66S1corv2, m // frequency of police contact by type of police contact

tab Q66RI1cori2, m // frequency last police contact by type of police contact

tab contactcori2, m // dummy at least one contact
tab pascontactcori2, m // count variable 0 contacts
tab NRcontactcori2, m // count variable NR
tab nbcontactcori2, m // count variable number of contacts
tab dernierContact2, m // RECODED frequency last police contact by type of police contact contact de r�f�rence pour les questions Q67 � Q74


tab contactS1, m // dummy for each contact

tab ContactPOL, m // dummy police initiated contacts                    
tab ContactIND, m // dummy self initiated contacts                      
tab nbcontactPOL, m // number police initiated contacts                  
tab nbcontactIND, m // number self initiated contacts                    
tab nbtypcontactPOL, m // recoded??     
tab nbtypcontactIND, m // recoded??                
tab r3CPOL, m  // recoded??         
                          
tab r3CIND, m  // recoded?? 
*/

*********************************************
*cleaning of type of contact and last contact  
********************************************
*fre Q66S1corv2-Q66S8corv2
*fre Q66RI1cori2-Q66RI8cori2
*fre Q66S8rec
*fre dernierContact2

* GENERATE NEW VARIABLES

foreach v of var Q66S1corv2 Q66S2corv2 Q66S3corv2 Q66S4corv2 Q66S5corv2 Q66S6corv2 Q66S7corv2 Q66S8corv2 {
	gen `v'r=`v'
}

foreach v in Q66RI1cori2 Q66RI2cori2 Q66RI3cori2 Q66RI4cori2 Q66RI5cori2 Q66RI6cori2 Q66RI7cori2 Q66RI8cori2 {
	gen `v'r=`v'
}

gen Q66S8recr= Q66S8rec
gen dernierContact2r=dernierContact2

*REPLACE VALUES OF CONTACT "other reasons" (Q66S1corv2 to Q66S8corv2)=Q66S8recr if they are the same => already done?

fre Q66S8recr
fre Q66S8rec 

forvalues i=1/7 {
		replace Q66S`i'corv2r= 1  if Q66S8rec==`i' & (Q66S`i'corv2r==0 | Q66S`i'corv2r==.)
}

* TRUNCATION OF VALUES

forvalues i=1/8 {
		fre Q66S`i'corv2r
}

recode Q66S4corv2r (20/max=20)
recode Q66S6corv2r (50/max=50)
recode Q66S7corv2r (50/max=50)
recode Q66S8corv2r (10/max=10)

*EQUALIZING INFORMATION LAST CONTACT AND SETTING MISSING VALUES=0 IF AT LEAST ONE CONTACT

forvalues i=1/8 {
	replace Q66S`i'corv2r=1 if  Q66RI`i'cori2r==1 & (Q66S`i'corv2r==0 | Q66S`i'corv2r==.)
}

foreach v of var Q66S1corv2r Q66S2corv2r Q66S3corv2r Q66S4corv2r Q66S5corv2r Q66S6corv2r Q66S7corv2r Q66S8corv2r {
	recode `v' (.=99)
}

egen Q66Smiss = anycount (Q66S1corv2r Q66S2corv2r Q66S3corv2r Q66S4corv2r Q66S5corv2r Q66S6corv2r Q66S7corv2r Q66S8corv2r), values (99)
fre Q66Smiss

forvalues i=1/8 {
	replace Q66S`i'corv2r=0 if Q66Smiss<8 & Q66S`i'corv2r==99
	recode Q66S`i'corv2r(99=.)	
}


* incidence of police contacts by type of contact

foreach v of var Q66S1corv2r-Q66S8corv2r {
	fre `v'
}


*all contacts

egen Q66Ssum=rowtotal(Q66S1corv2r-Q66S8corv2r), missing
recode Q66Ssum (0=0) (1=1) (2=2) (3/4=3) (5/max = 4), gen (Q66Ssumc)
lab var Q66Ssum "police contacts last year - incid."
lab var Q66Ssumc "police contacts last year - incid. categorical"

label define Q66Ssumlb 0 "none" 1 "once" 2 "twice" 3"3-4 times" 4 "more than four times" , replace

label val Q66Ssumc Q66Ssumlb

fre Q66Ssum
fre Q66Ssumc

*police initiated contacts (4/6/7)

egen Q66Spol=rowtotal (Q66S4corv2r Q66S6corv2r Q66S7corv2r), missing
recode Q66Spol (0=0) (1=1) (2=2) (3/4=3) (5/max = 4), gen (Q66Spolc)
lab var Q66Spol "police initiated contacts last year (4/6/7) - incid."
lab var Q66Spolc "police initiated contacts last year (4/6/7) - incid. categorical"

label val Q66Spolc Q66Ssumlb

fre Q66Spol
fre Q66Spolc

*self initiated contacts (1/2/3/5)

egen Q66Sself= rowtotal (Q66S1corv2r Q66S2corv2r Q66S3corv2r Q66S5corv2r), missing
recode Q66Sself (0=0) (1=1) (2=2) (3/4=3) (5/max = 4), gen (Q66Sselfc)
lab var Q66Sself "self initiated contacts last year (1/2/3/5) - incid."
lab var Q66Sselfc "self initiated contacts last year (1/2/3/5) - incid. categorical"

label val Q66Sselfc Q66Ssumlb

fre Q66Sself
fre Q66Sselfc

tab Q66Spolc Q66Sselfc


*CLEANING OF MISSING VALUES IN Q66* , GENERATING COUNT VARIABLE FOR LAST CONTACT

fre Q66S1corv2
fre Q66S1corv2r
fre Q66RI1cori2r

egen Q66RIsum = anycount (Q66RI1cori2r Q66RI2cori2r Q66RI3cori2r Q66RI4cori2r Q66RI5cori2r Q66RI6cori2r Q66RI7cori2r Q66RI8cori2r), values (1)
fre Q66RIsum


replace Q66RI1cori2r=1 if (Q66S1corv2r>0 & Q66S1corv2r!=.)& Q66RIsum==0 & (Q66S2corv2r-Q66S8corv2r==0)
replace Q66RI2cori2r=1 if (Q66S2corv2r>0 & Q66S2corv2r!=.)& Q66RIsum==0 &  Q66S1corv2r==0 & (Q66S3corv2r-Q66S8corv2r==0)
replace Q66RI3cori2r=1 if (Q66S3corv2r>0 & Q66S3corv2r!=.)& Q66RIsum==0 & (Q66S1corv2r-Q66S2corv2r==0) & (Q66S4corv2r-Q66S8corv2r==0)
replace Q66RI4cori2r=1 if (Q66S4corv2r>0 & Q66S4corv2r!=.)& Q66RIsum==0 & (Q66S1corv2r-Q66S3corv2r==0) & (Q66S5corv2r-Q66S8corv2r==0)
replace Q66RI5cori2r=1 if (Q66S5corv2r>0 & Q66S5corv2r!=.)& Q66RIsum==0 & (Q66S1corv2r-Q66S4corv2r==0) & (Q66S6corv2r-Q66S8corv2r==0)
replace Q66RI6cori2r=1 if (Q66S6corv2r>0 & Q66S6corv2r!=.)& Q66RIsum==0 & (Q66S1corv2r-Q66S5corv2r==0) & (Q66S7corv2r-Q66S8corv2r==0)
replace Q66RI7cori2r=1 if (Q66S7corv2r>0 & Q66S7corv2r!=.)& Q66RIsum==0 & (Q66S1corv2r-Q66S6corv2r==0) & (Q66S8corv2r-Q66S8corv2r==0)
replace Q66RI8cori2r=1 if (Q66S8corv2r>0 & Q66S8corv2r!=.)& Q66RIsum==0 & (Q66S1corv2r-Q66S7corv2r==0)

egen Q66RIsumr = anycount (Q66RI1cori2r Q66RI2cori2r Q66RI3cori2r Q66RI4cori2r Q66RI5cori2r Q66RI6cori2r Q66RI7cori2r Q66RI8cori2r), values (1)
fre Q66RIsumr


**************************************
*filter variable for last contact
***********************************

fre Q66Ssumc
fre Q66Sself
fre Q66Spol

fre Q66RIsumr

gen Q66last_filter=-1

replace Q66last_filter=99 if Q66Ssumc==.
replace Q66last_filter=0 if Q66Ssumc==0 & Q66Ssumc!=.
replace Q66last_filter=1 if Q66RIsumr==0 & Q66Spol>0 & Q66Sself==0 & Q66S8corv2r==0 & Q66Ssumc!=.
replace Q66last_filter=1 if Q66RIsumr==0 & Q66Sself>0 & Q66Spol==0 & Q66S8corv2r==0 & Q66Ssumc!=.
replace Q66last_filter=2 if Q66RIsumr==0 & Q66Spol>0 & Q66Sself==0 & Q66S8corv2r>0 & Q66Ssumc!=.
replace Q66last_filter=2 if Q66RIsumr==0 & Q66Sself>0 & Q66Spol==0 & Q66S8corv2r>0 & Q66Ssumc!=.
replace Q66last_filter=2 if Q66RIsumr==0 & Q66Sself>0 & Q66Spol>0 & Q66Ssumc!=.
replace Q66last_filter=1 if Q66RIsumr==1 & Q66Ssumc!=.
replace Q66last_filter=1 if Q66RIsumr>0 & Q66Spol>0 & Q66Sself==0 & Q66S8corv2r==0 & Q66Ssumc!=.
replace Q66last_filter=1 if Q66RIsumr>0 & Q66Sself>0 & Q66Spol==0 & Q66S8corv2r==0 & Q66Ssumc!=.
replace Q66last_filter=2 if Q66RIsumr>0 & Q66Spol>0 & Q66Sself==0 & Q66S8corv2r>0 & Q66Ssumc!=.
replace Q66last_filter=2 if Q66RIsumr>0 & Q66Sself>0 & Q66Spol==0 & Q66S8corv2r>0 & Q66Ssumc!=.
replace Q66last_filter=2 if Q66RIsumr>0 & Q66Sself>0 & Q66Spol>0 & Q66Ssumc!=.

fre Q66last_filter


label var  Q66last_filter "filter categories last police contact"
label define Q66last_filterlb 99 "missing" 0 "no  police contact" 1 "one type of police contact" 2 "several types of police contacts", replace 
label value Q66last_filter Q66last_filterlb

fre Q66last_filter

**************************************
*cleaning of question block Q67- Q74
*************************************
summarize Q67cori Q68S1cori Q68S2cori Q69cori Q70I1cori-Q70I8cori  Q71detailcori Q71S1cori-Q71S5cori Q72I1cori-Q72I6cori Q73S1cori Q73S2cori Q74cori

* scale police behaviour

tab Q71S5cori Q71S3cori

gen Q71S5corir=Q71S5cori
recode Q71S5corir (1/2=4) if Q71S3cori==1  | Q71S3cori==2


recode Q71S1cori (4=1) (3=2) (2=3) (1=4), gen(Q71S1corif)
recode Q71S3cori (4=1) (3=2) (2=3) (1=4), gen(Q71S3corif)

/*
drop spbeh

polychoric Q71S1corif Q71S3corif Q71S4cori Q71S5corir
global N = r(sum_w)
matrix r = r(R)
factormat r, pcf n($N) mineigen(1)
predict spbeh
label var spbeh "police behaviour(higher=better)"
recode spbeh (.=99)

recode spbeh (1/2.5=0) (2.51/max=2), gen(spbehd) 
label var spbehd "police behaviour - dichotomous"

tab spbeh, m

*/

*generating missing values for Q70* and Q72

foreach v of var Q70I1cori-Q70I8cori {
	gen `v'r=`v'
}

foreach v of var Q72I1cori-Q72I6cori {
	gen `v'r=`v'
}

egen q70miss= anycount (Q70I1cori-Q70I8cori), values (0)
fre q70miss

foreach v of var Q70I1corir-Q70I8corir {
	replace `v'=99 if q70miss==8
	*recode `v' (99=.)
}


egen q72miss= anycount (Q72I1cori-Q72I6cori), values (0)
fre q72miss

foreach v of var Q72I1corir-Q72I6corir {
	replace `v'=99 if q72miss==6
	*recode `v' (99=.)
}

*analyzing missing values of question block Q67- Q74

foreach v of var Q67cori Q68S1cori spbeh Q68S2cori Q69cori Q73S1cori Q73S2cori Q74cori {
	gen `v'r=`v'
	recode `v'r (.=99)
}

egen Q6774miss= anycount (Q67corir Q68S1corir Q68S2corir Q69corir Q70I1corir spbehr Q72I1corir Q73S1corir Q73S2corir Q74corir), values (99)
fre Q6774miss

summarize Q67cori Q68S1cori Q68S2cori Q69cori Q70I1cori-Q70I8cori Q71S1cori-Q71S5cori Q72I1cori-Q72I6cori Q73S1cori Q73S2cori Q74cori if Q6774miss==8
fre Q67corir if Q6774miss==8
fre Q68S1corir if Q6774miss==8

**************************************
*filter variable for question block Q67-Q74
***********************************
fre Q6774miss

gen Q67Q74_filter=-1
replace Q67Q74_filter= 1 if Q6774miss <= 5
replace Q67Q74_filter= 99 if Q6774miss>5

label var Q67Q74_filter "filter Q67Q74: validity questions last contact"
label define Q67Q74_filterlb  99 "questions last contact missing" 1"questions last contact valid", replace
label value Q67Q74_filter Q67Q74_filterlb

fre Q67Q74_filter

**************************************
*filter variable for question block Q66-Q74
***********************************

fre Q66last_filter 
fre Q67Q74_filter

list Q66S1corv2r-Q66S8corv2r if Q66RIsumr==0 & Q67Q74_filter==1

tab  Q66RIsumr Q67Q74_filter
tab  Q66Ssumc Q67Q74_filter, m


fre Q66last_filter 
fre Q67Q74_filter
tab  Q66last_filter Q67Q74_filter, m

gen Q66Q74_filter=-1

replace Q66Q74_filter=99 if Q67Q74_filter==99 & Q66last_filter ==99
replace Q66Q74_filter=0 if Q67Q74_filter==99 & Q66last_filter ==0

replace Q66Q74_filter=1 if Q67Q74_filter==99 & (Q66last_filter ==1 | Q66last_filter ==2)

*replace Q66Q74_filter=2 if Q67Q74_filter==1 & (Q66last_filter ==0 | Q66last_filter ==99)

replace Q66Q74_filter=2 if Q67Q74_filter==1 & (Q66last_filter ==2)
replace Q66Q74_filter=3 if Q67Q74_filter==1 & (Q66last_filter ==1)

label var Q66Q74_filter "filter Q66Q74: final filter for last contact"
label define Q66Q74_filterlb 99 "police contact missing + no valid information about contact" ///
0 "filter not valid: no police contact" ///
1 "filter not valid: one or more police contacts" ///
2 "filter valid: several types of police contacts" ///
3 "filter valid: one type of police contacts", replace 
label value Q66Q74_filter Q66Q74_filterlb

fre Q66Q74_filter


/*
replace Q66Q74_filter=99 if Q66last_filter==99 
replace Q66Q74_filter=0 if Q66last_filter==0 
replace Q66Q74_filter=99 if (Q66last_filter==1 | Q66last_filter==2) & Q67Q74_filter==99
replace Q66Q74_filter=1 if Q66last_filter==1 & Q67Q74_filter==1
replace Q66Q74_filter=2 if Q66last_filter==2 & Q67Q74_filter==1

label var Q66Q74_filter "filter Q66Q74: final filter for last contact"
label define Q66Q74_filterlb 99 "police contacts missing or questions last contact not valid" 0"no police contact" 1 "question block valid: one type of police contact" 2 "question block valid: several types of police contacts", replace
label value Q66Q74_filter Q66Q74_filterlb
*/


**************************************
*generate scales with filter
***********************************

gen Q66Ssumt= Q66Ssum
replace Q66Ssumt = 99 if Q66Q74_filter==99
replace Q66Ssumt =0 if Q66Q74_filter==0
replace Q66Ssumt =0 if Q66Q74_filter==2
recode Q66Ssumt (0=0) (1=1) (2=2) (2.01/4=3) (5/max = 4), gen (Q66Ssumct)

gen Q66Spolt= Q66Spol
replace Q66Spolt = 99 if Q66Q74_filter==99
replace Q66Spolt =0 if Q66Q74_filter==0
replace Q66Spolt =0 if Q66Q74_filter==2
recode Q66Spolt (0=0) (1=1) (2=2) (2.01/4=3) (5/max = 4), gen (Q66Spolct)

gen Q66Sselft= Q66Sself
replace Q66Sselft = 99 if Q66Q74_filter==99
replace Q66Sselft =0 if Q66Q74_filter==0
replace Q66Sselft =0 if Q66Q74_filter==2
recode Q66Sselft (0=0) (1=1) (2=2) (2.01/4=3) (5/max = 4), gen (Q66Sselfct)


*review

fre Q66Ssumc
fre Q66last_filter
fre Q67Q74_filter
fre Q66Q74_filter

fre Q66last_filter Q66Ssumc  

*set more on
*list Q66Spol Q66Sself Q66S8corv2r if Q66last_filter==1 & Q66Ssumc > 1

*save data

save "../../STATAfiles/FR_POLIS_31juillet14_FINAL&IRIS_pcnt.dta", replace



